package fy.hr.operator.impl;

import java.util.ArrayList;
import java.util.List;

import com.seeyon.apps.collaboration.manager.ColManager;
import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.services.ServiceException;
import com.seeyon.v3x.services.V3XLocator;
import com.seeyon.v3x.services.flow.bean.FlowExport;

import fy.form.listen.operate.AForm_Save_Operator;
import fy.form.listen.operate.IFormOperator;
import fy.hr.bean.AwayFormBean;
import fy.hr.constant.OA2HRConstant;
import fy.hr.dao.AwayFormDao;
import fy.util.MyUtil;

public class AbroadApplicationFormOperator extends AForm_Save_Operator implements
		IFormOperator {

	private AwayFormBean bean = new AwayFormBean();

	private List<String> awaybegindayList = new ArrayList<String>();

	private List<String> awayenddayList = new ArrayList<String>();

	private List<String> personCodeList = new ArrayList<String>();

	private StringBuilder awayAddressBuilder = new StringBuilder();
	
	@Override
	protected void parseOtherField_FirstStep(FlowExport flowExport) {

		bean.setSummaryId(super.formBean.getSummaryId());

		bean.setBillname(flowExport.getFlowTitle());

		bean.setApplier(flowExport.getFlowCreatePerson().getName());

		bean.setTs(flowExport.getFlowCreateTime());
	}

	@Override
	protected void parseValueExport(com.seeyon.v3x.services.form.bean.ValueExport ve) {
		String fieldName = ve.getDisplayName();

		if ("出国事由".equals(fieldName)) {
			bean.setAwaynote(ve.getValue());
		} else if ("申请日期".equals(fieldName)) {
			bean.setApplydate(MyUtil.getDate(ve.getValue()));
		} else if ("单据编号".equals(fieldName)) {
			bean.setAwayid(ve.getValue());
		} else if ("起始时间".equals(fieldName)) {
			awaybegindayList.add(MyUtil.getDate(ve.getValue()));
		} else if ("结束时间".equals(fieldName)) {
			awayenddayList.add(MyUtil.getDate(ve.getValue()));
		} else if ("出差人员工号".equals(fieldName)) {
			personCodeList.add(ve.getValue());
		} else if ("拟到访国家地区".equals(fieldName)) {
			awayAddressBuilder.append(ve.getValue()).append("，");
		}
	}

	public String getFormName() {
		return "因公出国（境）申请表";
	}

	public boolean save() {
		try {
			bean.setAwaybeginday(MyUtil.getEarlyDay(awaybegindayList));
			bean.setAwayendday(MyUtil.getLastDay(awayenddayList));
			bean.setPersonCodeList(personCodeList);
			bean.setAwayaddress(awayAddressBuilder.toString());
			bean.accountAwayDays();
			bean.setAheadfee("0");
			bean.setAwaytype(OA2HRConstant.AWAYTYPEARRAY[3]);
			this.bean.createNewNote();

			AwayFormDao afd = new AwayFormDao();

			afd.setChuChaiBean(bean);

			
			
			try {
				return afd.insertChuChaiInfo(String.valueOf(V3XLocator.getInstance().lookup(ColManager.class).getSummaryById(bean.getSummaryId()).getOrgAccountId()));
			} catch (BusinessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			return false;

		} finally {
			if (awaybegindayList != null)
				awaybegindayList.clear();
			awaybegindayList = null;
			if (awayenddayList != null)
				awayenddayList.clear();
			awayenddayList = null;

			if (personCodeList != null)
				personCodeList.clear();
			personCodeList = null;

			awayAddressBuilder = null;

			bean.clear();
			bean = null;
		}
	}

}
